package com.tarena.lbs.basic.web.service.impl;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.api.PicUpdateParam;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.dao.repository.StoreRespository;
import com.tarena.lbs.basic.web.service.BusinessService;
import com.tarena.lbs.basic.web.service.StoreService;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.dos.StoreDO;
import com.tarena.lbs.pojo.basic.param.StoreParam;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.ReferenceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreRespository storeRespository;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private AttachApi attachApi;

    @Override
    public BasePageList<StoreBO> storeList(StoreQuery query) {
        log.debug("获取商家分页信息Service入参:{}",query);
        return storeRespository.getStoreList(query);
    }

    @Override
    public void saveStore(StoreDO storeDO, StoreParam storeParam) throws BusinessException {
        log.debug("新增商家信息Service层入参 StoreDO:{}",storeDO);
        //1.根据BusinessStore 验证BusinessId是否合格
        checkBusiness(storeDO.getBusinessId());
        //2.调用仓储层
        Integer storeId = storeRespository.saveStore(storeDO);
        //3.绑定2个业务图片 店铺Logo业务200
        bindPictures(storeParam,storeId);
    }

    @Override
    public BasePageList<StoreBO> getCitiesStores(AreaStoreQuery query) throws BusinessException{
        //1.获取用户登录信息
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("101","用户解析认证失败");
        }
        log.debug("查询满足当前登录用户所隶属商家范围入参:{}AreaStoreQuery :{}",userPrinciple.getId(),query);
        //2.查询满足当前登录用户所隶属商家范围
        BasePageList<StoreBO> resultBos = storeRespository.getStoresByAreasAndBusiness(userPrinciple.getId(),query);
        return resultBos;
    }

    private void bindPictures(StoreParam storeParam, Integer storeId) {
        List<PicUpdateParam> picUpdateParams = new ArrayList<>();
        //封装 storeParam 传进来的 店铺图片
        List<String> storeImagesIds = storeParam.getStoreImagesIds();
        if (!CollectionUtils.isEmpty(storeImagesIds)){
            picUpdateParams = storeImagesIds.stream().map(picId->{
                PicUpdateParam param = new PicUpdateParam();
                param.setBusinessType(300);
                param.setBusinessId(storeId);
                param.setId(Integer.valueOf(picId));
                return param;
            }).collect(Collectors.toList());
        }
        //storeParam 提交店铺的入参中,拿到Logo图片的Id
        Integer logoId = Integer.valueOf(storeParam.getStoreLogo());
        PicUpdateParam logoParam = new PicUpdateParam();
        logoParam.setId(logoId);
        logoParam.setBusinessId(200);
        logoParam.setBusinessType(storeId);
        picUpdateParams.add(logoParam);
        attachApi.batchUpdateBusiness(picUpdateParams);
    }

    private void checkBusiness(Integer businessId) throws BusinessException {
        //判断是否存在
        boolean exists= businessService.existsBusiness(businessId);
        if (!exists){
            throw new BusinessException("105","商家根本不存在,无法新增店铺");
        }
    }
}
